package com.snail.readyvr.di.module;

import android.content.Context;

import com.snail.readyvr.mvp.model.DaoMaster;
import com.snail.readyvr.mvp.model.DaoSession;
import com.snail.readyvr.rest.LoginRequestInterceptor;
import com.snail.readyvr.rest.LoginRestClient;
import com.snail.readyvr.rest.RequestInterceptor;
import com.snail.readyvr.rest.RestClient;

import org.greenrobot.greendao.database.Database;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

/**
 * Created by MRKING on 2016/10/10.
 */
@Module
public class ApplicationModule {
    private Context mContext;
    public final static String HOST = "http://epg.readyvr.woniucloud.com/";//线上
   //public final static String HOST = "http://192.168.5.225:3000/";//测试

    private final static String LOGIN_HOST = "http://passport.readyvr.woniucloud.com/";//线上
    //private final static String LOGIN_HOST = "http://192.168.5.137:3050/";//测试

    public final static String DANMU_HOST = "gift.readyvr.woniucloud.com";//线上
    //public final static String DANMU_HOST = "192.168.5.137";//测试

    public final static int DANMU_HOST_PORT = 80;//线上
    //public final static int DANMU_HOST_PORT = 8166;//测试


    public ApplicationModule(Context context) {
        mContext = context;
    }
    @Singleton
    @Provides
    public Context provideContext() {
        return mContext;
    }


    @Singleton
    @Provides
    public RestClient provideRestClient(){

        /*HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);//正式发布时去掉*/

        OkHttpClient client = new OkHttpClient.Builder()
                                .connectTimeout(5, TimeUnit.SECONDS)
                                .readTimeout(10, TimeUnit.SECONDS)
                                .retryOnConnectionFailure(true)
                                .addInterceptor(new RequestInterceptor())
                               // .addInterceptor(logging)//正式发布时去掉
                                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HOST)
                .client(client)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(RestClient.class);

    }
    @Singleton
    @Provides
    public LoginRestClient provideLoginRestClient(){

       /* HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);//正式发布时去掉*/

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .addInterceptor(new LoginRequestInterceptor())
               // .addInterceptor(logging)//正式发布时去掉
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(LOGIN_HOST)
                .client(client)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(LoginRestClient.class);

    }

    @Singleton
    @Provides
    public DaoSession provideDaoSession(){
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext,"readyvr-db");
        Database db = helper.getWritableDb();
        return new DaoMaster(db).newSession();
    }

}
